package com.example.liu_test.config;

import com.auth0.jwt.JWT;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * token工具类
 * @Title: TokenUtil
 * @author lw
 * @date 2021/07/06
 */
public class TokenUtil {
    /**
     * @return 返回的是当前登录人的name
     */
    public static String getTokenUserId() {
        Cookie[] cookies = getRequest().getCookies();
        System.out.println("---------------");
        for(int i = 0;i<cookies.length;i++){
            if(cookies[i].getName().equals("nms_username")){
                System.out.println("+++++++");
                return cookies[i].getValue();
            }
        }
        return "wu";
    }
    /**
     * 获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        return requestAttributes == null ? null : requestAttributes.getRequest();
    }
}

